#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：prod_automater_requests_test 
@File    ：proposer.py
@IDE     ：PyCharm 
@Author  ：XiaoDengYa
@Date    ：2021/6/26/026 13:35 
"""

import json
import sys
import os

sys.path.insert(1, os.path.dirname(os.path.dirname(os.path.abspath(""))))
from common.utils.read_util import OperationJson, OperationIni
from common.utils.request_util import RestClient


class ApplicantPromoter(object):
    def __init__(self, log, ip):
        self.log = log
        self.ip = ip
        self.re = RestClient(log, ip)
        self.json = OperationJson()
        self.url = self.json.get_own_json("api_url.json")
        self.header = self.json.get_own_json("user_header.json")
        self.ini = OperationIni("setting.ini").get_ini_file_path_section_data('user')

    def promoter_submit(self):
        json_data = self.json.get_own_json("promoter_test.json")["promoter_submit"]
        json_data["real_name"] = "dxt" + self.ini.get("account")[-4:]
        json_data["mobile_phone"] = self.ini.get("account")
        json_data["vlog_account"] = "dxt" + self.ini.get("account")[-4:]

        header = self.header.get("promoter")
        res = self.re.requests(url=self.url.get("promoter_submit"), method="POST", data=json.dumps(json_data),
                               headers=header)
        if "application/json" in res.headers.get("Content-Type"):
            self.log.info('[ response json ] : %s' % res.json())
        else:
            self.log.info('[ response text ] : %s' % res.text)

        if "apply_id" in res.json():
            apply_id = res.json()["apply_id"]
            return res, apply_id
        else:
            return False

    def promoter_audit(self, apply_id):
        json_data = self.json.get_own_json("promoter_test.json")["promoter_audit"]
        header = self.header.get("proposer")
        url = self.url.get("promoter_audit")
        new_url = url.replace("{}", str(apply_id))
        res = RestClient(self.log, "proposer_pass").requests(url=new_url, method="POST", data=json.dumps(json_data),
                                                             headers=header)
        if "application/json" in res.headers.get("Content-Type"):
            self.log.info('[ response json ] : %s' % res.json())
        else:
            self.log.info('[ response text ] : %s' % res.text)
        if "check_id" in res.json():
            return res
        else:
            return False

    def promoter_cash_deposit(self, apply_id):
        header = self.header.get("proposer")
        url = self.url.get("promoter_cash_deposit")
        new_url = url.replace("{}", str(apply_id))
        RestClient(self.log, "proposer_pass").requests(url=new_url, method="PUT", headers=header)


if __name__ == '__main__':
    from common.utils.log_util import GenerateLog

    print(ApplicantPromoter(GenerateLog().logger(), "proposer_pass").promoter_audit("207"))
